package com.fgs.common.widget.itemView;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.R;
import com.fgs.common.utils.CommonUtil;
import com.xuexiang.xui.widget.textview.badge.Badge;
import com.xuexiang.xui.widget.textview.badge.BadgeView;
import com.xuexiang.xui.widget.textview.label.LabelView;
import com.xuexiang.xutil.app.AppUtils;


/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class SettingItemView extends LinearLayout {
    private ImageView mLeftImageView;
    private ImageView mRightImageView;
    private TextView mTipTextView;
    private TextView mTitleTextView;
    private TextView mSubTitleTextView;
    private TextView mContentTextView;
    private TextView mSubContentTextView;
    private ImageView mArrowImageView;
    private String mTitle;
    private String mSubTitle;
    private String mContent;
    private String mContentHint;
    private String mSubContent;
    private int mTitleTextColor;
    private int mContentTextColor;
    private int mSubContentTextColor;
    private int mContextHintColor;
    private int mLeftIcon;
    private int mRightIcon;
    private int mLeftIconWidth;
    private int mLeftIconHeight;
    private int mLeftIconPadding;
    private int mRightIconWidth;
    private int mRightIconHeight;
    private boolean isShowArrow;
    private boolean isShowTipText;
    private boolean isShowLeftIcon;
    private boolean isShowRightIcon;

    private Badge mBadge;
    public SettingItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
        mTitle = typedArray.getString(R.styleable.SettingItemView_SettingItemTitle);
        mSubTitle = typedArray.getString(R.styleable.SettingItemView_SettingItemSubTitle);
        mContent = typedArray.getString(R.styleable.SettingItemView_SettingItemContent);
        mSubContent = typedArray.getString(R.styleable.SettingItemView_SettingItemSubContent);
        mContentHint = typedArray.getString(R.styleable.SettingItemView_SettingItemContentHint);
        mTitleTextColor = typedArray.getColor(R.styleable.SettingItemView_SettingItemTitleTextColor, context.getResources().getColor(R.color.colorTextBlack));
        mContentTextColor = typedArray.getColor(R.styleable.SettingItemView_SettingItemContentTextColor, context.getResources().getColor(R.color.colorTextPrimary));
        mSubContentTextColor = typedArray.getColor(R.styleable.SettingItemView_SettingItemSubContentTextColor, context.getResources().getColor(R.color.colorTextPrimary));
        mContextHintColor = typedArray.getColor(R.styleable.SettingItemView_SettingItemContentHintTextColor, context.getResources().getColor(R.color.colorTextGray));

        mLeftIcon = typedArray.getResourceId(R.styleable.SettingItemView_SettingItemLeftIcon, 0);
        mRightIcon = typedArray.getResourceId(R.styleable.SettingItemView_SettingItemRightIcon, 0);
        isShowArrow = typedArray.getBoolean(R.styleable.SettingItemView_SettingItemIsShowArrow, true);
        isShowTipText = typedArray.getBoolean(R.styleable.SettingItemView_SettingItemIsShowTip, false);
        isShowLeftIcon = typedArray.getBoolean(R.styleable.SettingItemView_SettingItemIsShowLeftIcon, false);
        isShowRightIcon = typedArray.getBoolean(R.styleable.SettingItemView_SettingItemIsShowRightIcon, false);

        mLeftIconWidth = typedArray.getDimensionPixelSize(R.styleable.SettingItemView_SettingItemLeftIconWidth, (int) CommonUtil.convertDpToPixel(18));
        mLeftIconHeight = typedArray.getDimensionPixelSize(R.styleable.SettingItemView_SettingItemLeftIconHeight, (int) CommonUtil.convertDpToPixel(18));
        mLeftIconPadding = typedArray.getDimensionPixelSize(R.styleable.SettingItemView_SettingItemLeftIconPadding, 0);

        mRightIconWidth = typedArray.getDimensionPixelSize(R.styleable.SettingItemView_SettingItemRightIconWidth, (int) CommonUtil.convertDpToPixel(18));
        mRightIconHeight = typedArray.getDimensionPixelSize(R.styleable.SettingItemView_SettingItemRightIconHeight, (int) CommonUtil.convertDpToPixel(18));


        typedArray.recycle();

        View view = LayoutInflater.from(context).inflate(R.layout.layout_setting_item, null);
//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) AppUtils.convertDpToPixel(40));
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        mTipTextView = view.findViewById(R.id.layout_setting_item_tipTextView);
        mTitleTextView = view.findViewById(R.id.layout_setting_item_titleTextView);
        mSubTitleTextView = view.findViewById(R.id.layout_setting_item_subTitleTextView);
        mContentTextView = view.findViewById(R.id.layout_setting_item_contentTextView);
        mSubContentTextView = view.findViewById(R.id.layout_setting_item_subContentTextView);
        mLeftImageView = view.findViewById(R.id.layout_setting_item_leftImageView);
        mRightImageView = view.findViewById(R.id.layout_setting_item_rightImageView);
        mArrowImageView = view.findViewById(R.id.layout_setting_item_rightArrowImageView);

        setTitle(mTitle);
        setSubTitle(mSubTitle);
        setContent(mContent);
        setSubContent(mSubContent);
        setContentHint(mContentHint);
        setTitleTextColor(mTitleTextColor);
        setContentTextColor(mContentTextColor);
        setSubContentTextColor(mSubContentTextColor);
        setContextHintColor(mContextHintColor);
        setShowArrow(isShowArrow);
        setShowTip(isShowTipText);
        setLeftIcon(mLeftIcon);
        setShowLeftIcon(isShowLeftIcon);
        setRightIcon(mRightIcon);
        setShowRightIcon(isShowRightIcon);

        setLeftIconSize(mLeftIconWidth, mLeftIconHeight);
        setRightIconSize(mRightIconWidth, mRightIconHeight);

    }

    //设置提示数量
    public void setBadgeNumber(int number) {
        if (mLeftImageView.getVisibility() == View.VISIBLE) {
            if (mBadge == null) {
                mBadge = new BadgeView(getContext())
                        .bindTarget(mLeftImageView)
                        .setShowShadow(false)
                        .setBadgeBackgroundColor(getResources().getColor(R.color.colorRed))
                        .setBadgeGravity(Gravity.TOP | Gravity.END)
                        .setBadgeTextColor(getResources().getColor(R.color.colorWhite))
                        .setBadgeTextSize(10, true);
            }

            mBadge.setBadgeNumber(number);
        }
    }

    public void setItemClickable(boolean isClickable) {
        setClickable(isClickable);
        setEnabled(isClickable);
    }


    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
        mTitleTextView.setText(title);
    }

    public void setSubTitle(String subTitle) {
        this.mSubTitle = subTitle;
        if (!TextUtils.isEmpty(subTitle)) {
            mSubTitleTextView.setVisibility(View.VISIBLE);
        } else {
            mSubTitleTextView.setVisibility(View.GONE);
        }
        mSubTitleTextView.setText(subTitle);
    }

    public String getContent() {
        return mContent;
    }

    public void setContent(String content) {
        this.mContent = content;
        mContentTextView.setText(content);
    }

    public void setSubContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            mSubContentTextView.setVisibility(View.VISIBLE);
        } else {
            mSubContentTextView.setVisibility(View.GONE);
        }
        this.mSubContent = content;
        mSubContentTextView.setText(content);
    }

    public String getContentHint() {
        return mContentHint;
    }

    public void setContentHint(String contentHint) {
        this.mContentHint = contentHint;
        mContentTextView.setHint(contentHint);
    }

    public int getTitleTextColor() {
        return mTitleTextColor;
    }

    public void setTitleTextColor(int titleTextColor) {
        this.mTitleTextColor = titleTextColor;
        mTitleTextView.setTextColor(titleTextColor);
    }

    public int getContentTextColor() {
        return mContentTextColor;
    }

    public void setContentTextColor(int contentTextColor) {
        this.mContentTextColor = contentTextColor;
        mContentTextView.setTextColor(contentTextColor);
    }

    public void setSubContentTextColor(int textColor) {
        this.mSubContentTextColor = textColor;
        mSubContentTextView.setTextColor(textColor);
    }

    public void setTextWeiZhi(int gravity) {

        mContentTextView.setGravity(gravity);
    }


    public int getContextHintColor() {
        return mContextHintColor;
    }

    public void setContextHintColor(int contextHintColor) {
        this.mContextHintColor = contextHintColor;
        mContentTextView.setHintTextColor(contextHintColor);
    }

    public int getLeftIcon() {
        return mLeftIcon;
    }

    public void setLeftIcon(int leftIcon) {
        this.mLeftIcon = leftIcon;
        if (leftIcon > 0) {
            mLeftImageView.setImageResource(leftIcon);
        } else {
            mLeftImageView.setImageResource(0);
        }
    }

    public boolean isShowArrow() {
        return isShowArrow;
    }

    public void setShowArrow(boolean showArrow) {
        isShowArrow = showArrow;
        if (showArrow) {
            mArrowImageView.setVisibility(View.VISIBLE);
        } else {
            mArrowImageView.setVisibility(View.INVISIBLE);
        }
    }

    public boolean isShowLeftIcon() {
        return isShowLeftIcon;
    }

    public void setShowLeftIcon(boolean showLeftIcon) {
        isShowLeftIcon = showLeftIcon;
        if (showLeftIcon) {
            mLeftImageView.setVisibility(View.VISIBLE);
        } else {
            mLeftImageView.setVisibility(View.GONE);
        }
    }

    public int getRightIcon() {
        return mRightIcon;
    }

    public void setRightIcon(int rightIcon) {
        this.mRightIcon = rightIcon;
        if (rightIcon > 0) {

            mRightImageView.setBackgroundResource(rightIcon);
        } else {

            mRightImageView.setBackgroundResource(0);
        }
    }

    public boolean isShowRightIcon() {
        return isShowRightIcon;
    }


    private void setShowTip(boolean isShowTipText) {

        this.isShowTipText = isShowTipText;
        if (isShowTipText) {
            mTipTextView.setVisibility(View.VISIBLE);
        } else {
            mTipTextView.setVisibility(View.GONE);
        }
    }

    public void setShowRightIcon(boolean showRightIcon) {
        isShowRightIcon = showRightIcon;
        if (showRightIcon) {
            mRightImageView.setVisibility(View.VISIBLE);
        } else {
            mRightImageView.setVisibility(View.GONE);
        }
    }

    public void setLeftIconSize(int width, int height) {
        LayoutParams layoutParams = (LayoutParams) mLeftImageView.getLayoutParams();
//        layoutParams.width = (int) CommonUtil.convertDpToPixel(width);
//        layoutParams.height = (int) CommonUtil.convertDpToPixel(height);
        layoutParams.width = width;
        layoutParams.height = height;
        mLeftImageView.setPadding(mLeftIconPadding, mLeftIconPadding, mLeftIconPadding, mLeftIconPadding);
        mLeftImageView.setLayoutParams(layoutParams);
    }

    public void setRightIconSize(int width, int height) {
        LayoutParams layoutParams = (LayoutParams) mRightImageView.getLayoutParams();
//        layoutParams.width = (int) CommonUtil.convertDpToPixel(width);
//        layoutParams.height = (int) CommonUtil.convertDpToPixel(height);
        layoutParams.width = width;
        layoutParams.height = height;
        mRightImageView.setLayoutParams(layoutParams);
    }


}
